<cfparam name="resetFilters" default="false">
<cfset isFiltered = false />
<cfset queryParams = StructNew() />

<!--- approved --->
<cfif NOT IsDefined("client.workshops.approved") or resetFilters>
	<cfset client.workshops.approved = "All">
<cfelseif IsDefined("filterApproved")>
	<cfset client.workshops.approved = filterApproved>
</cfif>
<cfif client.workshops.approved neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.Approved = client.workshops.approved />
</cfif>

<!--- Program_Year --->
<cfif NOT IsDefined("client.workshops.Program_Year") or resetFilters>
	<cfset client.workshops.Program_Year = request.currentProgramYear>
<cfelseif IsDefined("filterProgramYear")>
	<cfset client.workshops.Program_Year = filterProgramYear>
</cfif>
<cfif client.workshops.Program_Year neq request.currentProgramYear>
	<cfset isFiltered = true />
	<cfset queryParams.Program_Year = client.workshops.Program_Year />
	<!--- 
	override filterPeriod when filterProgramYear before currentProgramYear
	CAUTION: This block is order-of-operation critical.  This block must come before the filter block for filterPeriod
	 --->
	<cfset filterPeriod = "All" />
<cfelse>
	<cfset queryParams.Program_Year = request.currentProgramYear />
</cfif>

<!--- period --->
<!--- <cfif NOT IsDefined("client.workshops.period") or resetFilters>
	<cfset client.workshops.period = "All">
<cfelseif IsDefined("filterPeriod")>
	<cfset client.workshops.period = filterPeriod />
</cfif>
<cfif client.workshops.period neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.period = client.workshops.period />
</cfif> --->
<cfset client.workshops.period = "All" />

<!--- online --->
<cfif NOT IsDefined("client.workshops.online") or resetFilters>
	<cfset client.workshops.online = "All">
<cfelseif IsDefined("filterOnline")>
	<cfset client.workshops.online = filterOnline />
</cfif>
<cfif client.workshops.online neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.Online = client.workshops.online />
</cfif>

<!--- organizationIDs --->
<cfif NOT IsDefined("client.workshops.organizationIDs") or resetFilters>
	<cfset client.workshops.organizationIDs = "All">
<cfelseif IsDefined("filterOrganizationIDs")>
	<cfset client.workshops.organizationIDs = filterOrganizationIDs />
</cfif>
<cfif client.workshops.organizationIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.organizationIDs = client.workshops.organizationIDs />
</cfif>

<!--- regionIDs --->
<cfif IsUserInRole("Super User")>
	<cfif NOT IsDefined("client.workshops.regionIDs") or resetFilters>
		<cfset client.workshops.regionIDs = "All">
	<cfelseif IsDefined("filterRegionIDs")>
		<cfset client.workshops.regionIDs = filterRegionIDs />
	</cfif>
<cfelseif IsUserInRole("Regional Admin")>
	<cfset client.workshops.regionIDs = request.authUser.regionID />
</cfif>
<cfif client.workshops.regionIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.regionIDs = client.workshops.regionIDs />
</cfif>

<cfwddx action="cfml2wddx" input="#queryParams#" output="client.workshops.queryParamsWDDX" />

<cfscript>
	workshopGateway = new assets.calgrants.com.gateway.WorkshopGateway();
	workshops = workshopGateway.query(argumentCollection = queryParams);
	organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
	organizations = organizationGateway.query(regionIDs = IIf(IsUserInRole("Regional Admin"), request.authUser.regionID, ""), Organization_ID = IIf(IsUserInRole("Organizer") OR IsUserInRole("Site Contact"), request.authUser.organizationID, ""));
	storedProcedureGateway = new assets.calgrants.com.gateway.StoredProcedureGateway();
	regions = storedProcedureGateway.call("getDistinctRegions");
</cfscript>

<!--- <cfdump var="#_workshops#">
<cfabort> --->

<cfoutput>

	<h3>Workshops<cfif isFiltered> <small><em>filtered</em></small></cfif></h3>
	
	<div class="row-fluid" style="margin-bottom: 10px;">
		<div class="span12">
			<a href="#script_name#?view=editWorkshop&Workshop_ID=0" class="btn">New&nbsp;workshop</a>
			<a id="filterLink" href="##" class="btn" onclick="toggleFilter('filter')">Show filters</a>
			<div class="btn-group">
				<button class="btn dropdown-toggle" data-toggle="dropdown">Actions</button>
				<button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
				<ul class="dropdown-menu">
					<cfif workshops.RecordCount gt 0>
						<li><a href="?action=export&collection=workshops" target="_new">Export</a></li>
					</cfif>
				</ul>
			</div>
			<span class="pull-right"><strong>Count:</strong> #workshops.RecordCount#</span>
		</div>
	</div>

	<cfform name="workshopsForm" id="workshopsForm" action="#script_name#" method="post">
		<cfinput type="hidden" name="view" value="#view#">

		<div id="filter" class="alert alert-info alert-block" style="display: none;">
			<h4>Filters</h4>
			<table cellpadding="6">
				<tr valign="top">
					<td>
						<label>Program Year</label>
						<cfselect name="filterProgramYear" size="1" multiple="false" required="yes" message="Program Year filter is required" style="width: 150px;">
							<cfloop index="y" from="2008" to="#ListFirst(request.currentProgramYear, '-')#">
								<cfset py = "#y#-" & y+1>
								<option value="#py#"<cfif client.workshops.Program_Year eq py> selected</cfif>>#py#</option>
							</cfloop>
						</cfselect>
					</td>
					<cfif IsUserInRole("Super User")>
						<td>
							<label>Regions</label>
							<cfselect name="filterRegionIDs" size="7" multiple="true" required="yes" message="Region filter is required" style="width: 150px;">
								<option value="All"<cfif client.workshops.regionIDs eq "All"> selected</cfif>>All</option>
								<cfloop query="regions">
									<option value="#Region_ID#"<cfif ListFind(client.workshops.regionIDs, Region_ID)> selected</cfif>>#Region_ID#</option>
								</cfloop>
							</cfselect>
						</td>				
					</cfif>
					<td>
						<label>Organizations</label>
						<cfselect name="filterOrganizationIDs" size="7" multiple="true" required="yes" message="Organization filter is required" style="width: auto;">
							<option value="All"<cfif client.workshops.organizationIDs eq "All"> selected</cfif>>All</option>
							<cfloop query="organizations">
							<option value="#Organization_ID#"<cfif ListFind(client.workshops.organizationIDs, Organization_ID)> selected</cfif>>#Organization# - #City#</option>
							</cfloop>
						</cfselect>
					</td>
					<!--- <td>
						<label>Period</label>
						<cfif client.workshops.Program_Year eq request.currentProgramYear>
							<cfselect name="filterPeriod" size="1" required="yes" message="Period filter is required" style="width: 150px;">
								<option value="All"<cfif client.workshops.period eq "All"> selected</cfif>>All</option>
								<option value="All Past"<cfif client.workshops.period eq "All Past"> selected</cfif>>All Past</option>
								<option value="Past Week"<cfif client.workshops.period eq "Past Week"> selected</cfif>>Past Week</option>
								<option value="Yesterday"<cfif client.workshops.period eq "Yesterday"> selected</cfif>>Yesterday</option>
								<option value="Today"<cfif client.workshops.period eq "Today"> selected</cfif>>Today</option>
								<option value="Tomorrow"<cfif client.workshops.period eq "Tomorrow"> selected</cfif>>Tomorrow</option>
								<option value="Next Week"<cfif client.workshops.period eq "Next Week"> selected</cfif>>Next Week</option>
								<option value="All Future"<cfif client.workshops.period eq "All Future"> selected</cfif>>All Future</option>
							</cfselect>
						<cfelse>
							<span class="muted"><small>Not available as filtered</small></span>
						</cfif>
					</td> --->
					<td>
						<label>Computer Based</label>
						<cfselect name="filterOnline" size="1" multiple="false" required="yes" message="Computer Based filter is required" style="width: 150px;">
							<option value="All"<cfif client.workshops.online eq "All"> selected</cfif>>All</option>
							<option value="Yes"<cfif client.workshops.online eq "Yes"> selected</cfif>>Yes</option>
							<option value="No"<cfif client.workshops.online eq "No"> selected</cfif>>No</option>
						</cfselect>
					</td>
					<td>
						<label>Status</label>
						<cfselect name="filterApproved" size="1" multiple="false" required="yes" message="Status filter is required" style="width: 150px;">
							<option value="All"<cfif client.workshops.approved eq "All"> selected</cfif>>All</option>
							<option value="Yes"<cfif client.workshops.approved eq "Yes"> selected</cfif>>Approved</option>
							<option value="No"<cfif client.workshops.approved eq "No"> selected</cfif>>Not approved</option>
						</cfselect>
					</td>
				</tr>
				<tr>
					<td colspan="5">
						<button type="submit" class="btn btn-info">Apply</button> <a href="#script_name#?view=workshops&resetFilters=true" class="btn btn-info">Clear</a>
					</td>
				</tr>
			</table>
		</div>
		
		<cfif workshops.RecordCount gt 0>

			<table id="workshopsList" style="display: none; width: 100%">
				<thead>
					<tr>
						<th>ID</th>
						<th>Program Year</th>
						<th>Organization</th>
						<cfif IsUserInRole("Super User")>
							<th>Region</th>
						</cfif>
						<th>Site</th>
						<th>Date/Time</th>
						<!--- <th>Organizer</th>
						<th>Site Contact</th> --->
						<cfif IsUserInRole("Super User")>
							<th>Computer Based</th>
						</cfif>
						<th>Exit Surveys (W/P)</th>
						<th>Follow Up Requests</th>
						<th>Status</th>
					</tr>
				</thead>
				<tbody>
					<cfloop query="workshops">
						<!--- <cfset unverifiedStudentCount = 0 />
						<cfif PB_Survey_Count gt 0 and Student_Count_PB eq 0>
							<cfset unverifiedStudentCount = PB_Survey_Count />
						<cfelseif PB_Survey_Count gt 0 and Student_Count_PB gt 0>
							<cfset unverifiedStudentCount = PB_Survey_Count - Student_Count_PB />
						</cfif>
						<cfset totalStudentCount = Student_Count + unverifiedStudentCount /> --->
						<!--- <cfif Student_Count_PB eq 0 AND PB_Survey_Count gt 0>
							<cfset studentCount = Student_Count_Web + PB_Survey_Count />
						<cfelse>
							<cfset studentCount = Student_Count_Web + Student_Count_PB />
						</cfif> --->
						<tr>
							<td>
								<cfif Program_Year gte "2011-2012">
									<a href="#script_name#?view=editWorkshop&Workshop_ID=#Workshop_ID#">#Workshop_ID#</a>
								<cfelse>
									#Workshop_ID#
								</cfif>								
							</td>
							<td>#Program_Year#</td>
							<td>
								<cfif Organization_Deleted>
									<font color="red">#Organization#</font>
								<cfelseif Organization_Archived>
									<font color="silver">#Organization#</font>
								<cfelseif NOT Organization_Approved>
									<font color="orange">#Organization#</font>
								<cfelseif Organization_Approved>
									#Organization#
								</cfif>
							</td>
							<cfif IsUserInRole("Super User")>
								<td>#Region_ID#</td>
							</cfif>
							<td>#Site#</td>
							<td><span class="sortHide">#DateFormat(Workshop_Date, 'yyyymmdd')##TimeFormat(Start_Time, 'HHmm')#</span>#DateFormat(Workshop_Date, "m/d/yyyy")# #TimeFormat(Start_Time, "h:mmtt")#&nbsp;-&nbsp;#TimeFormat(End_Time, "h:mmtt")#</td>
							<!--- <td>
								<cfif Organizer_Deleted>
									<font color="red">#Organizer_Full_Name#</font>
								<cfelseif Organizer_Archived>
									<font color="silver">#Organizer_Full_Name#</font>
								<cfelseif NOT Organizer_Approved>
									<font color="orange">#Organizer_Full_Name#</font>
								<cfelseif Organizer_Approved>
									#Organizer_Full_Name#
								</cfif>
							</td>
							<td>
								<cfif Manager_Deleted>
									<font color="red">#Manager_Full_Name#</font>
								<cfelseif Manager_Archived>
									<font color="silver">#Manager_Full_Name#</font>
								<cfelseif NOT Manager_Approved>
									<font color="orange">#Manager_Full_Name#</font>
								<cfelseif Manager_Approved>
									#Manager_Full_Name#
								</cfif>
							</td> --->
							<!--- <cfif IsUserInRole("Super User")>
								<td>#Int(wbStudentCount)#</td>
								<cfif (pbStudentCount gt 0 or PB_Survey_Count gt 0) AND pbStudentCount neq PB_Survey_Count>
									<cfset pbConflict = true>
								<cfelse>
									<cfset pbConflict = false>
								</cfif>
								<td><cfif pbConflict eq true><span style="color: red;"></cfif>#Int(pbStudentCount)#<cfif pbStudentCount gt 0 or PB_Survey_count gt 0> (#PB_Survey_Count#)</cfif><cfif pbConflict eq true></span></cfif></td>
								<!--- <td>
									<cfif PB_SURVEY_COUNT_STATUS neq "Uninitiated">
										#PB_SURVEY_COUNT_STATUS#
									</cfif>
								</td>
								<td>#dateformat(PB_SHIPPED_DATE, "m/d/yyyy")#</td> --->
							</cfif> --->
							<cfif IsUserInRole("Super User")>
								<td>
									<cfif Program_Year gte "2012-2013">
										#YesNoFormat(Online)#
									<cfelse>
										<span class="muted">N/A</span>
									</cfif>
								</td>
							</cfif>
							<td><span class="sortHide"><cfif Len(Student_Count) eq 1>00#Student_Count#<cfelseif Len(Student_Count) eq 2>0#Student_Count#<cfelse>#Student_Count#</cfif></span>#Student_Count# (#Student_Count_Web#/<cfif Student_Count_PB eq 0 and PB_Survey_Count gt 0>#PB_Survey_Count#<cfelse>#Student_Count_PB#</cfif>)</td>
							<!--- <td><span class="sortHide"><cfif Len(Student_Count) eq 1>00#Student_Count#<cfelseif Len(Student_Count) eq 2>0#Student_Count#<cfelse>#Student_Count#</cfif></span>#Student_Count# (#Student_Count_Web#/<cfif Student_Count_PB eq 0 AND PB_Survey_Count gt 0>#PB_Survey_Count#<cfelse>#Student_Count_PB#</cfif>)</td> --->
							<td><span class="sortHide"><cfif Len(Follow_Up_Request_Count) eq 1>00#Follow_Up_Request_Count#<cfelseif Len(Follow_Up_Request_Count) eq 2>0#Follow_Up_Request_Count#<cfelse>#Follow_Up_Request_Count#</cfif></span>#Follow_Up_Request_Count#</td>			
							<td>
								<cfif Deleted>
									<font color="red">Deleted</font>
								<cfelseif NOT Approved>
									<font color="orange">Not Approved</font>
								<cfelseif Approved>
									Approved
								</cfif>
							</td>
						</tr>
					</cfloop>
				</tbody>
			</table>
			
			<script type="text/javascript">
				$(function() {
					$("##workshopsList").kendoGrid({
						sortable: {
							mode: "single",
							allowUnsort: false
						}		
					})
					$("##workshopsList").show();
				})
			</script>
			
		<cfelse>
		
			<div class="alert alert-info notFound">No workshops</div>
	
			<script type="text/javascript" language="javascript">
			<!--
				$(function() {
					$("##filter").show();
				});
			// -->
			</script>

		</cfif>

	</cfform>

</cfoutput>